package org.example.link_list;

public class Swapping_nodes_in_a_linked_list_in_pairs {
    public static void main(String[] args) {
        //两两交换链表中的节点
        //给你一个链表，两两交换其中相邻的节点，并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题（即，只能进行节点交换）
        ListNode listNode2 = new ListNode(5);
        listNode2.next = null;
        ListNode listNode3 = new ListNode(4,listNode2);
        ListNode listNode4 = new ListNode(3,listNode3);
        ListNode listNode5 = new ListNode(2,listNode4);
        ListNode head = new ListNode(1,listNode5);
        ListNode virtualHead = new ListNode(0);
        virtualHead.next = head;
        ListNode before = null;
        ListNode after = null;
        ListNode current = virtualHead;
        while(current.next!=null && current.next.next!=null){
            before = current.next;
            after = current.next.next;
            current.next.next = after.next;
            after.next = before;
            current.next = after;
            current = current.next.next;
        }
        ListNode w = virtualHead.next;
        while (w !=null ){
            System.out.print(w.val);
            w = w.next;
        }
    }
}
